//
//  DBManager.m
//  parent
//
//  Created by jr on 2018/1/18.
//  Copyright © 2018年 com.jiazhanghui. All rights reserved.
//

#import "DBManager.h"
#import "AddressAreaModel.h"
#import "NSArray+Array.h"
@implementation DBManager
+ (DBManager *)instance {
    static DBManager *_instance = nil;
    
    @synchronized (self) {
        if (_instance == nil) {
            _instance = [[self alloc] init];
        }
    }
    return _instance;
}
-(void)getDBProviceData:(NSString *)parentId dataArray:(DataArray)array{
    
     NSString *filenameAgo = [[NSBundle mainBundle] pathForResource:@"address" ofType:@"db"];
    FMDatabase *db = [FMDatabase databaseWithPath:filenameAgo];
    if ([db open]) {
        NSMutableArray *arr = [NSMutableArray array];
        NSString *sql = [NSString stringWithFormat:@"select * from address_dict where parentId = '%@'",parentId];
        FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {
            int userId = [rs intForColumn:@"id"];
            NSString *name = [rs stringForColumn:@"name"];
            int pass = [rs intForColumn:@"parentId"];
            AddressAreaModel *model = [[AddressAreaModel alloc]init];
            model.sh_id = [NSString stringWithFormat:@"%d",userId];
            model.sh_name = name;
            model.sh_parentid = [NSString stringWithFormat:@"%d",pass];
            [arr addObject:model];
        }
        array(arr);
        [db close];
    }
}
-(void)getParent:(NSString *)cityId id:(ParentId)parentId{
    NSString *filenameAgo = [[NSBundle mainBundle] pathForResource:@"address" ofType:@"db"];
    FMDatabase *db = [FMDatabase databaseWithPath:filenameAgo];
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select * from address_dict where id = '%@'",cityId];
        FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {
           
            int pass = [rs intForColumn:@"parentId"];
            if (pass == 0) {
                return;
            }
            parentId(pass);
        }
        [db close];
    }
}
@end
